<template>
  <div id="home">
    <nav-bar class="home-nav-bar">
      <div slot="center">Home</div>
    </nav-bar>
    <tab-control :ctitles="['流行', '新款', '精选']" 
                @tabClick="pTabClick" ref="tabControl1"
                class="tab-control"
                v-show="isTabFixed"
    />
    <scroller class="home-scroller" 
              ref="scroller" 
              :probe-type="3" 
              @scroll="getPostion" 
              :pull-up-load="true"
              @pullUpLoad="loadMore"
    >
      <home-swiper :cbanners="banners" @homeSwiperImgLoad="homeSwiperImgLoad"></home-swiper>
      <home-recommend :crecommends="recommends"></home-recommend>
      <home-feature-view></home-feature-view>
      <tab-control 
                  :ctitles="['流行', '新款', '精选']" 
                  @tabClick="pTabClick" ref="tabControl2" />
      <goods-list :cgoods="goods[this.currentType].list" />
    </scroller>
    <back-top @click.native="backTopClick" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
import NavBar from 'components/common/navbar/NavBar'
import Scroller from 'components/common/scroller/Scroller'
import TabControl from 'components/content/tabControl/TabControl'
import GoodsList from 'components/content/good/GoodsList'
import {backTopMixin} from 'common/mixin'

import HomeSwiper from './childComps/HomeSwiper'
import HomeRecommend from './childComps/HomeRecommend'
import HomeFeatureView from './childComps/HomeFeatureView'

import {getHomeMultiData, getGoodsData} from "network/home";
import {debounce} from 'common/utils'
export default {
  name: "Home",
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: {page: 0, list: []},
        new: {page: 0, list: []},
        sell: {page: 0, list: []}
      },
       currentType:'pop',
       tabOffsetTop: 0,
       isTabFixed: false,
       saveY: 0,
       itemImgListener: null
    }
  },
  mixins: [backTopMixin],
  components: {
    NavBar,
    Scroller,
    HomeSwiper,
    HomeRecommend,
    HomeFeatureView,
    TabControl,
    GoodsList
  },
  computed: {
    // showGoods() {
    //   return this.goods[this.currentType].list
    // }
  },
  activated() {
    this.$refs.scroller.refresh()
    this.$refs.scroller.scroller.scrollTo(0,this.saveY,0)
  },
  deactivated() {
    //1.保存Y值
    this.saveY = this.$refs.scroller.getScrollY()

    //2.取消全局事件的监听
    this.$bus.$off('itemImgLoad', this.itemImgListener)
  },
  created() {
    this.getHomeData()

    this.getHomeGoodsData('pop')
    this.getHomeGoodsData('new')
    this.getHomeGoodsData('sell')
  },
  mounted() {
    // this.$bus.$on('imgLoad', () => {
    //   this.$refs.scroller.refresh()
    // })
    //1.监听图片加载完成
    let newRefresh = debounce(this.$refs.scroller.refresh, 100)
    this.itemImgListener = () => {
      newRefresh()
    }
    this.$bus.$on('imgLoad', this.itemImgListener)

    //2.获取tabContorl的offsetTop属性
    //所有的组件都有一个属性$el: 用于获取组件中的元素
    // console.log(this.$refs.tabControl.$el.offsetTop);
  },
  methods: {
    getPostion(postion) {
      //1.判断BackTop是否显示
        this.isShowBackTop = -postion.y > 300
      //2.决定tabControl是否吸顶(position: fixed)
        this.isTabFixed = (-postion.y) > this.tabOffsetTop
    },
    loadMore() {
      // console.log('上拉加载更多');
      this.getHomeGoodsData(this.currentType)
    },

    pTabClick(index) {
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index; 
    },
    homeSwiperImgLoad() {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
    }


    ,
    /**
     * 网络请求 
     * */
    getHomeData() {
      getHomeMultiData().then(res => {
        this.banners = res.data.banner.list
        this.recommends = res.data.recommend.list
      })
    },
    getHomeGoodsData(type) {
      const page = this.goods[type].page + 1
      getGoodsData(type, page).then(res => {
        this.goods[type].list.push(...res.data.list)
        this.goods[type].page +=1
        this.$refs.scroller.finishPullUp()
     })
    }
  }
}
</script>

<style>
  #home {
    /* padding-top: 44px; */
    height: 100vh;
    position: relative;
  }

  .home-nav-bar {
    background-color: var(--color-tint);
    color:#fff;

    /* 在使用浏览器原声滚动时，为了让导航不跟随一起滚动 */
    /* position: fixed;
    left: 0;
    right:0;
    top: 0;
    z-index: 9; */
  }

  .home-scroller{
    /* height:300px; */
    overflow: hidden; 
    position: absolute;
    top: 44px;
    bottom: 49px;
    right: 0;
    left: 0;
  }
  .tab-control {
    position: relative;
    z-index: 9;
  }

  /* .home-tab-control {
  
  } */
</style>